package com.homer.android.sdk.command.user;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.homer.android.sdk.type.user.EspLoginResult;
import com.homer.android.sdk.type.user.EspUserbindDeviceResult;
import com.homer.android.sdk.type.user.builder.BEspUser;
import com.homer.iot.esptouch.base.api.EspBaseApiUtil;

public class EspCommandUserBindDeviceInternet implements IEspCommandUserBindDeviceInternet{

	private final static Logger log = Logger.getLogger(EspCommandUserBindDeviceInternet.class);
	@Override
	public EspUserbindDeviceResult doCommandUserBindDeviceInternet(String deviceNumbers,String bssid) {
		 try
	        {
	            JSONObject jsonObject = new JSONObject();
	            jsonObject.put("deviceNumber", deviceNumbers);
	            jsonObject.put("bssid", bssid);
	            jsonObject.put("userToken", BEspUser.getBuilder().getInstance().getUserKey());	           
	            JSONObject jsonObjectResult = EspBaseApiUtil.Post(URL, jsonObject);
	            EspUserbindDeviceResult result = null;
	            if (jsonObjectResult == null)
	            {
	                log.debug(Thread.currentThread().toString() + "##doCommandUserBindDeviceInternet(deviceNumber=[" + deviceNumbers
	                    + "],userToken=[" + BEspUser.getBuilder().getInstance().getUserKey() + "]): " + EspLoginResult.NETWORK_UNACCESSIBLE);
	                return EspUserbindDeviceResult.NETWORK_UNACCESSIBLE;
	            }
	            int status = jsonObjectResult.getInt("status");
	          
	            result = EspUserbindDeviceResult.getEspLoginResult(status);
	            log.debug(Thread.currentThread().toString() + "##doCommandUserBindDeviceInternet(deviceNumber=[" + deviceNumbers
	                    + "],userToken=[" + BEspUser.getBuilder().getInstance().getUserKey() + "]): " + EspLoginResult.NETWORK_UNACCESSIBLE);
	            return result;
	        }
	        catch (JSONException e)
	        {
	            e.printStackTrace();
	        }
	       
	        return EspUserbindDeviceResult.NETWORK_UNACCESSIBLE;
	}

}
